package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.util.IdGen;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbRotation;
import com.bsb.base.model.BsbRotationExample;
import com.bsb.base.model.BsbRotationExample.Criteria;
import com.bsb.base.model.SysUser;
import com.bsb.biz.service.BsbRotationService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbRotation")
public class BsbRotationController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbRotation/list";
	
	@Resource
	private BsbRotationService bsbRotationService;
	
	@RequiresPermissions("bsbRotation:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			model.addAttribute("msg", "账号异常请联系管理员！");
			
			return "/BsbRotation/list";
		}
		BsbRotationExample bsbRotationExample = new BsbRotationExample();
		Criteria createCriteria = bsbRotationExample.createCriteria();
		createCriteria.andFirmIdEqualTo(firmId.intValue());
		int totalCount = bsbRotationService.countBsbRotation(bsbRotationExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbRotationExample.setPageIndex((pageNum - 1) * pageSize);
		bsbRotationExample.setPageSize(pageSize);
		bsbRotationExample.setOrderByClause("createTime DESC");
		List<BsbRotation> bsbRotations = bsbRotationService.getPageBsbRotation(bsbRotationExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("firmId", firmId);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbRotations", bsbRotations);
		return "/BsbRotation/list";
	}
	
	@RequiresPermissions("bsbRotation:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model,RedirectAttributes redirectAttributes) {
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			redirectAttributes.addFlashAttribute("msg", "账号异常请联系管理员！");
			return redirect;
		}
		model.addAttribute("firmId", firmId);
		return "/BsbRotation/add";
	}

	@RequiresPermissions("bsbRotation:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model,RedirectAttributes redirectAttributes, @ModelAttribute("bsbRotation") BsbRotation bsbRotation) {
		SysUser currentUser = UserUtils.currentUser();
		String username = currentUser.getUsername();
		bsbRotation.setUuid(IdGen.uuid());
		bsbRotation.setCreateBy(username);
		bsbRotation.setCreateTime(new Date());
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			redirectAttributes.addFlashAttribute("msg", "账号异常请联系管理员！");
			return redirect;
		}
		if(firmId!=10000){
			bsbRotation.setType("0");// 0 首页 1 直播预告
		}
		bsbRotation.setIsNative("1");//0 原生  1  富文本   2 H5
		bsbRotation.setFirmId(firmId.intValue());
		bsbRotationService.addBsbRotation(bsbRotation);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return redirect;
	}
	
	@RequiresPermissions("bsbRotation:del")
	@RequestMapping(value = "/{uuid}/del", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		
		BsbRotation bsbRotation = bsbRotationService.getBsbRotationByUuid(uuid);
		if(bsbRotation!=null){
			SysUser currentUser = UserUtils.currentUser();
			Long firm = currentUser.getFirmId();
			
			Integer firmId = bsbRotation.getFirmId();
			if(firm==null||firmId==null){
				redirectAttributes.addFlashAttribute("msg", "账号异常请联系管理员！");
			}
			if(firm.intValue()!=firmId){
				redirectAttributes.addFlashAttribute("msg", "失败 ！");
			}else {
				bsbRotationService.delBsbRotationByUuid(uuid);
				redirectAttributes.addFlashAttribute("msg", "删除成功");
			}
			
		}else {
			redirectAttributes.addFlashAttribute("msg", "失败 ！");
		}
		String queryString = request.getQueryString();
		if(StringUtils.isEmpty(queryString)){
			queryString = "";
		}
		return redirect + "?" +queryString;
	}
	
	@RequiresPermissions("bsbRotation:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		BsbRotation bsbRotation = bsbRotationService.getBsbRotationByUuid(uuid);
		model.addAttribute("bsbRotation", bsbRotation);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			redirectAttributes.addFlashAttribute("msg", "账号异常请联系管理员！");
			return redirect;
		}
		model.addAttribute("firmId", firmId);
		return "/BsbRotation/edit";
	}

	@RequiresPermissions("bsbRotation:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,
			@RequestParam(value = "queryString", required = false) String queryString,
			RedirectAttributes redirectAttributes, @ModelAttribute("bsbRotation") BsbRotation bsbRotation) {
		SysUser currentUser = UserUtils.currentUser();
		String username = currentUser.getUsername();
		bsbRotation.setUpdateBy(username);
		bsbRotation.setUpdateTime(new Date());
		bsbRotationService.updateBsbRotation(bsbRotation);
		redirectAttributes.addFlashAttribute("msg", "修改成功");

		if(StringUtils.isEmpty(queryString)){
			queryString = "";
		}
		return redirect + "?" +queryString;
	}
	
	@RequiresPermissions("bsbRotation:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbRotation bsbRotation = bsbRotationService.getBsbRotationByUuid(uuid);
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		model.addAttribute("firmId", firmId);
		model.addAttribute("bsbRotation", bsbRotation);
		return "/BsbRotation/view";
	}
	@RequiresPermissions("bsbRotation:preview")
	@RequestMapping(value = "/{uuid}/preview", method = RequestMethod.GET)
	public String preview(Model model,
			@PathVariable("uuid") String uuid) {
		BsbRotation bsbRotation = bsbRotationService.getBsbRotationByUuid(uuid);
		model.addAttribute("bsbRotation", bsbRotation);
		return "/BsbRotation/preview";
	}
}
